package com.example.mbl.controller;

import com.example.mbl.entity.*;
import com.example.mbl.result.PageResult;
import com.example.mbl.result.SearchPageResult;
import com.example.mbl.result.business.SearchResult;
import com.example.mbl.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * @Author liuhaoqi
 * @Date 2020/6/10 20:20
 * @Version 1.0
 */
@RestController
@RequestMapping("Search")
public class SearchController {

    @Autowired
    public NewsService newsService;
    @Autowired
    public EventService eventService;
    @Autowired
    public ArticleService articleService;
    @Autowired
    public PatentService patentService;
    @Autowired
    public BookService bookService;
    @Autowired
    RedisTemplate redisTemplate;

    @GetMapping("searchItemsByKeyWord")
    public SearchPageResult searchItemsByKeyWord(@RequestParam("keyword") String keyword,
                                                         @RequestParam(value = "pageNum",defaultValue = "1") int pageNum,
                                                         @RequestParam(value = "pageSize",defaultValue = "10") int pageSize) {

        SearchPageResult result = new SearchPageResult();
        if (keyword == null) {
            result.setPageSize(0);
            result.setMessage("当前页没有数据");
            result.setTotal(0);
            result.setPageIndex(pageNum);
            result.setPageCount(0);
            return result;
        }
        keyword=keyword.trim();
        if ("".equals(keyword)) {
            result.setPageSize(0);
            result.setMessage("当前页没有数据");
            result.setTotal(0);
            result.setPageIndex(pageNum);
            result.setPageCount(0);
            return result;
        }

        List<News> news = newsService.searchItemsByKeyWord(keyword);
        List<Event> events = eventService.searchItemsByKeyWord(keyword);
        List<Article> articles = articleService.searchItemsByKeyWord(keyword);
        List<Patent> patents = patentService.searchItemsByKeyWord(keyword);
        List<Book> books=bookService.searchItemsByKeyWord(keyword);

        List<Object> arrayList = new ArrayList<>();
        arrayList.addAll(news);
        arrayList.addAll(events);
        arrayList.addAll(articles);
        arrayList.addAll(patents);
        arrayList.addAll(books);
        int begin=pageSize * (pageNum - 1);
        int cursize=0;
        int total=arrayList.size();
        if (total - pageSize * (pageNum - 1) >= pageSize) {
            cursize = pageSize;
        } else {
            cursize = total - pageSize * (pageNum - 1);
        }

        if (cursize <= 0) {
            result.setPageIndex(pageNum);
            result.setResult(0);
            result.setTotal(total);
            result.setPageCount(total % pageSize == 0 ? total / pageSize : total / pageSize + 1);
            result.setMessage("当前页没有数据");
            result.setPageSize(0);
            return result;
        }


        List<Object> subList = arrayList.subList(begin, cursize+begin);
        ArrayList<News> newsList = new ArrayList<>();
        ArrayList<Event> eventList = new ArrayList<>();
        ArrayList<Article> articleList = new ArrayList<>();
        ArrayList<Patent> patentList = new ArrayList<>();
        ArrayList<Book> bookList = new ArrayList<>();

        for (int i = 0; i < cursize; i++) {
            if (subList.get(i).toString().contains("News")) {
                newsList.add((News) subList.get(i));
            }else if (subList.get(i).toString().contains("Event")) {
                eventList.add((Event) subList.get(i));
            }else if (subList.get(i).toString().contains("Article")) {
                articleList.add((Article)subList.get(i));
            }else if (subList.get(i).toString().contains("Patent")) {
                patentList.add((Patent) subList.get(i));
            }else if (subList.get(i).toString().contains("Book")) {
                bookList.add((Book) subList.get(i));
            }
        }
        SearchResult searchResult = new SearchResult();
        searchResult.setNews(newsList);
        searchResult.setEvents(eventList);
        searchResult.setArticles(articleList);
        searchResult.setPatents(patentList);
        searchResult.setBooks(bookList);
        result.setRows(searchResult);
        result.setPageIndex(pageNum);
        result.setResult(1);
        result.setTotal(total);
        result.setPageCount(total % pageSize == 0 ? total / pageSize : total / pageSize + 1);
        result.setMessage("获取当前页数据成功");
        result.setPageSize(cursize);
        return result;
    }
}
